<?php

    #-------------------------------------------------------------#
	# This file is part of the Tekuna MVC Microframework for PHP. #
	# (c) 2009 Niels Jäckel <niels.jaeckel@tekuna.org>            #
	# Please visit http://tekuna.org/ for more information.       #
	#                                                             #
	# This software is licensed under the MIT License. Please     #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#


	class ResponseTest extends AbstractTekunaTestCase {


		public function testGettersSetters() {

			// get instance
			$objResponse = new Response();

			// test getters and setters
			$sProtocol = 'HTTP/1.0';
			$objResponse -> setResponseProtocol($sProtocol);
			$this -> assertIdentical($objResponse -> getResponseProtocol(), $sProtocol);

			$iStatus = 201;
			$objResponse -> setStatus($iStatus);
			$this -> assertIdentical($objResponse -> getStatus(), $iStatus);

			$sContentType = 'text/plain';
			$objResponse -> setContentType($sContentType);
			$this -> assertIdentical($objResponse -> getContentType(), $sContentType);

			$sHeader = 'Cache-Control';
			$sValue = 'no-cache, must-revalidate';
			$this -> assertFalse($objResponse -> headerExists($sHeader));
			$objResponse -> setHeader($sHeader, $sValue);
			$this -> assertTrue($objResponse -> headerExists($sHeader));
			$this -> assertIdentical($objResponse -> getHeader($sHeader), $sValue);

			$sContent = 'myTestContent';
			$this -> assertIdentical($objResponse -> getContent(), '');
			$objResponse -> appendContent($sContent);
			$this -> assertIdentical($objResponse -> getContent(), $sContent);
			$objResponse -> resetContentBuffer();
			$this -> assertIdentical($objResponse -> getContent(), '');
		}


		public function testExceptions() {

			// get instance
			$objResponse = new Response();


			// the status must be valid
			try {

				$objResponse -> setStatus(999);
				$this -> fail();
			}
			catch (ResponseException $e) {

				$this -> pass();
			}


			// the content type
			try {

				$objResponse -> setContentType('notValidType');
				$this -> fail();
			}
			catch (ResponseException $e) {

				$this -> pass();
			}


			// check non-existent header
			try {

				$objResponse -> getHeader('myNonExistentHeader');
				$this -> fail();
			}
			catch (ResponseException $e) {

				$this ->pass();
			}
		}


		public function testDebugMessagesDisabled() {

			// start test buffer
			ob_start();

			// get instance
			$objResponse = new Response();

			// imitate a debug message within the code
			echo 'debug message.';

			// test default behavior: do not show debug messages
			$objResponse -> appendContent('regular output.');

			// suppress header warning that will be generated by ob_end_flush().
			// this output fills the test buffer
			@$objResponse -> sendResponse();

			// compare test buffer to expected output
			$this -> assertIdentical(ob_get_clean(), 'regular output.');
		}


		public function testDebugMessagesEnabled() {

			// start test buffer
			ob_start();

			// get instance
			$objResponse = new Response();

			// enable debug output sending
			$objResponse -> setSendDebugOutputs(TRUE);

			// imitate a debug message within the code
			echo 'debug message.';

			// test default behavior: do not show debug messages
			$objResponse -> appendContent('regular output.');

			// suppress header warning that will be generated by ob_end_flush().
			// this output fills the test buffer
			@$objResponse -> sendResponse();

			// compare test buffer to expected output
			$this -> assertIdentical(ob_get_clean(), 'debug message.regular output.');
		}
	}



